package com.xebest.web.controller.finance;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.xebest.api.util.OpertorLogUtil;
import com.xebest.common.base.controller.BASEMessage;
import com.xebest.common.base.controller.BaseController;
import com.xebest.common.base.controller.BaseResponse;
import com.xebest.common.enums.erp.finance.VenPrepayStatusEnum;
import com.xebest.common.enums.logtype.ErpLogTypeEnum;
import com.xebest.common.util.RequestUtil;
import com.xebest.util.SysSetBeanUtils;
import com.xebest.util.XeVenExcelUtil;
import com.xebest.util.msg.XeVenFinaceMsg;
import com.xebest.util.seq.FinanceSeqUtil;
import com.xebest.web.controller.export.XeVenFinPrepayBillExDTO;
import com.xebest.web.form.XeVenFinPrepayBillForm;
import com.xebest.web.form.XeVenFinPrepayBillQueryForm;
import com.xebest.web.form.XeVenFinPrepayCheckForm;
import com.xebest.web.model.base.XePageInfo;
import com.xebest.web.service.XeVenFinPrepayBillService;
import com.xebest.web.view.ErpUserInfo;
import com.xebest.web.view.SuppliersForPrepayView;
import com.xebest.web.view.XeVenFinPrepayBillPrintView;
import com.xebest.web.view.XeVenFinPrepayBillView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author 杜春静
 */
@RestController
@RequestMapping("finPrepayBill")
public class XeVenFinPrepayBillController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(XeVenFinPrepayBillController.class);
    //财务审核预付单时的标识
    private static final String FINMANAGER = "finManager";
    //财务部付款列表查询时的标识
    private static final String FINPAY = "finPay";
    @Autowired
    XeVenFinPrepayBillService prepayBillService;

    @Value("${xeven.erp.exportTempletDir}")
    private String exportTempletDir;// 导出文件目录

    @Value("${usercode.erp.pay}")
    private String laUsercode;

    /**
     * 采购部新增预付单
     *
     * @author 杜春静
     */
    @RequestMapping("/addPrepayBill")
    public @ResponseBody
    BaseResponse addPrepayBill(@RequestBody @Validated XeVenFinPrepayBillForm form, BindingResult bindingResult) {
        log.info("保存预付单基本信息请求参数为：{}" + JSON.toJSONString(form));
        // 验证参数
        if (null == form) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        if (bindingResult.hasErrors()) {
            return sendAjaxError(bindingResult.getFieldError().getDefaultMessage());
        }
        try {
            String operatorContent = JSON.toJSONString(form);
            //1.获取userCode
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            //  生成预付单号
            String prepayBillNo = FinanceSeqUtil.seqUtils.createPrepayBill();
            form.setPrepayBillNo(prepayBillNo);
            int result = prepayBillService.addPrepayBill(userInfo, form);
            this.insertPrepayMqLog(null, prepayBillNo, operatorContent);
            if (result > 0) {
                return this.sendAjaxOK();
            } else {
                return this.sendAjaxError();
            }

        } catch (Exception e) {
            log.error("添加预付单 信息出现异常：{}", e);
            return sendAjaxError();
        }

    }

    /**
     * 采购部修改预付单
     *
     * @author 杜春静
     */
    @RequestMapping("/editPrepayBill")
    public @ResponseBody
    BaseResponse editPrepayBill(@RequestBody @Validated XeVenFinPrepayBillForm form, BindingResult bindingResult) {
        log.info("编辑预付单基本信息请求参数为：{}" + JSON.toJSONString(form));
        // 验证参数
        if (null == form || StringUtils.isEmpty(form.getPrepayBillNo())) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        if (bindingResult.hasErrors()) {
            return sendAjaxError(bindingResult.getFieldError().getDefaultMessage());
        }
        //1.获取userCode
        try {
            String operatorContent = JSON.toJSONString(form);
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            int result = prepayBillService.editPrepayBill(userInfo, form);
            this.insertPrepayMqLog(null, form.getPrepayBillNo(), operatorContent);
            if (result > 0) {
                return this.sendAjaxOK();
            } else {
                return this.sendAjaxError();
            }
        } catch (Exception e) {
            log.error("编辑预付单 信息出现异常：{}", e);
            return sendAjaxError();
        }

    }

    /**
     * 采购部删除预付单
     *
     * @author 杜春静
     */
    @RequestMapping("/delPrepayBill")
    public @ResponseBody
    BaseResponse delPrepayBill(@RequestBody @Validated XeVenFinPrepayCheckForm form, BindingResult bindingResult) {
        log.info("删除预付单基本信息请求参数为：{}" + JSON.toJSONString(form));

        // 验证参数
        if (bindingResult.hasErrors()) {
            return sendAjaxError(bindingResult.getFieldError().getDefaultMessage());
        }
        if (null == form || StringUtils.isEmpty(form.getPrepayBills()) || !("DEL").equals(form.getOperateType())) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        try {

            String operatorContent = JSON.toJSONString(form);
            //1.获取userCode
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            //验证预付单是否是待审核未删除的单子
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("userCode", userInfo.getUserCode());
            param.put("prepayBills", form.getPrepayBills());
            param.put("prepayStatus", VenPrepayStatusEnum.CHECK_PENDING.getValue());
            int countPrepay = prepayBillService.countBillByParam(param);
            if (countPrepay < 0 || countPrepay != form.getPrepayBills().length) {
                return sendAjaxError(BASEMessage.PARAM_ERROR);
            }
            //批量删除预付单
            int result = prepayBillService.delPrepayBill(userInfo, form);
            this.insertPrepayMqLog(null, form.getPrepayBills().toString(), operatorContent);
            if (result < 0) {
                return this.sendAjaxError();
            }
        } catch (Exception e) {
            log.error("删除预付单异常{}", e);
            return this.sendAjaxError();
        }
        return this.sendAjaxOK();
    }

    /**
     * 采购部审核通过
     *
     * @author 杜春静
     */
    @RequestMapping("/checkPreBillPass")
    public @ResponseBody
    BaseResponse checkPrepayBillPass(@RequestBody @Validated XeVenFinPrepayCheckForm form, BindingResult bindingResult) {
        log.info("采购部审核通过预付单基本信息请求参数为：{}" + JSON.toJSONString(form));

        // 验证参数
        if (bindingResult.hasErrors()) {
            return sendAjaxError(bindingResult.getFieldError().getDefaultMessage());
        }
        if (null == form || StringUtils.isEmpty(form.getPrepayBills()) || !("CHECKYES").equals(form.getOperateType())) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        try {

            String operatorContent = "采购部审核通过";
            operatorContent += JSON.toJSONString(form);
            //1.获取userCode
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            //验证预付单是否是待审核未删除的单子
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("userCode", userInfo.getUserCode());
            param.put("prepayBills", form.getPrepayBills());
            param.put("prepayStatus", VenPrepayStatusEnum.CHECK_PENDING.getValue());
            int countPrepay = prepayBillService.countBillByParam(param);
            if (countPrepay < 0 || countPrepay != form.getPrepayBills().length) {
                return sendAjaxError(BASEMessage.PARAM_ERROR);
            }
            operatorContent += "审核人：" + userInfo.getLoginName();
            //批量审核预付单通过
            int result = prepayBillService.checkPrepayBillPass(userInfo, form);
            this.insertPrepayMqLog(null, form.getPrepayBills().toString(), operatorContent);
            if (result < 0) {
                return this.sendAjaxError();
            }
        } catch (Exception e) {
            log.error("采购部审核通过预付单异常{}", e);
            return this.sendAjaxError();
        }
        return this.sendAjaxOK();
    }

    /**
     * 采购部审核预付单不通过
     *
     * @author 杜春静
     */
    @RequestMapping("/checkPreBillRefuse")
    public @ResponseBody
    BaseResponse checkPrepayBillRefuse(@RequestBody @Validated XeVenFinPrepayCheckForm form, BindingResult bindingResult) {
        log.info("采购部审核不通过预付单基本信息请求参数为：{}" + JSON.toJSONString(form));

        // 验证参数
        if (bindingResult.hasErrors()) {
            return sendAjaxError(bindingResult.getFieldError().getDefaultMessage());
        }
        if (null == form || StringUtils.isEmpty(form.getPrepayBills()) || !("CHECKNO").equals(form.getOperateType())) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        try {
            String operatorContent = "采购部审核不通过";
            operatorContent += JSON.toJSONString(form);
            //1.获取userCode
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            //验证预付单是否是待审核未删除的单子
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("userCode", userInfo.getUserCode());
            param.put("prepayBills", form.getPrepayBills());
            param.put("prepayStatus", VenPrepayStatusEnum.CHECK_PENDING.getValue());
            int countPrepay = prepayBillService.countBillByParam(param);
            if (countPrepay < 0 || countPrepay != form.getPrepayBills().length) {
                return sendAjaxError(BASEMessage.PARAM_ERROR);
            }
            operatorContent += "审核人：" + userInfo.getLoginName();
            //批量审核预付单通过
            int result = prepayBillService.checkPrepayBillRefuse(userInfo, form);
            this.insertPrepayMqLog(null, form.getPrepayBills().toString(), operatorContent);
            if (result < 0) {
                return this.sendAjaxError();
            }
        } catch (Exception e) {
            log.error("采购部审核不通过预付单异常{}", e);
            return this.sendAjaxError();
        }
        return this.sendAjaxOK();
    }

    /**
     * 财务部审核预付单为通过
     *
     * @author 杜春静
     */
    @RequestMapping("/finCheckPreBillPass")
    public @ResponseBody
    BaseResponse finCheckPrepayBillPass(@RequestBody @Validated XeVenFinPrepayCheckForm form, BindingResult bindingResult) {
        log.info("财务部审核通过预付单基本信息请求参数为：{}" + JSON.toJSONString(form));

        // 验证参数
        if (bindingResult.hasErrors()) {
            return sendAjaxError(bindingResult.getFieldError().getDefaultMessage());
        }
        if (null == form || StringUtils.isEmpty(form.getPrepayBills()) || !("FINCHECKYES").equals(form.getOperateType())) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        try {
            String operatorContent = "财务部审核通过";
            operatorContent += JSON.toJSONString(form);
            //1.获取userCode
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            //验证预付单是否是财务待审核未删除的单子
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("userCode", userInfo.getUserCode());
            param.put("prepayBills", form.getPrepayBills());
            param.put("prepayStatus", VenPrepayStatusEnum.FINANCE_CHECK_PENDING.getValue());
            int countPrepay = prepayBillService.countBillByParam(param);
            if (countPrepay < 0 || countPrepay != form.getPrepayBills().length) {
                return sendAjaxError(BASEMessage.PARAM_ERROR);
            }
            //批量审核预付单通过
            int result = prepayBillService.finCheckPrepayBillPass(userInfo, form);
            operatorContent += "审核人：" + userInfo.getLoginName();
            this.insertPrepayMqLog(null, form.getPrepayBills().toString(), operatorContent);
            if (result < 0) {
                return this.sendAjaxError();
            }
        } catch (Exception e) {
            log.error("财务部审核通过预付单异常{}", e);
            return this.sendAjaxError();
        }
        return this.sendAjaxOK();
    }

    /**
     * 财务部审核不通过预付单
     *
     * @author 杜春静
     */
    @RequestMapping("/finCheckPreBillRefuse")
    public @ResponseBody
    BaseResponse finCheckPrepayBillRefuse(@RequestBody @Validated XeVenFinPrepayCheckForm form, BindingResult bindingResult) {
        log.info("财务部审核不通过预付单基本信息请求参数为：{}" + JSON.toJSONString(form));

        // 验证参数
        if (bindingResult.hasErrors()) {
            return sendAjaxError(bindingResult.getFieldError().getDefaultMessage());
        }
        if (null == form || StringUtils.isEmpty(form.getPrepayBills()) || !("FINCHECKNO").equals(form.getOperateType())) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        try {
            String operatorContent = "预付单财务部审核不通过";
            operatorContent += JSON.toJSONString(form);
            //1.获取userCode
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            //验证预付单是否是财务待审核未删除的单子
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("userCode", userInfo.getUserCode());
            param.put("prepayBills", form.getPrepayBills());
            param.put("prepayStatus", VenPrepayStatusEnum.FINANCE_CHECK_PENDING.getValue());
            int countPrepay = prepayBillService.countBillByParam(param);
            if (countPrepay < 0 || countPrepay != form.getPrepayBills().length) {
                return sendAjaxError(BASEMessage.PARAM_ERROR);
            }
            //批量审核预付单不通过
            int result = prepayBillService.finCheckPrepayBillRefuse(userInfo, form);
            operatorContent += "审核人：" + userInfo.getLoginName();
            this.insertPrepayMqLog(null, form.getPrepayBills().toString(), operatorContent);
            if (result < 0) {
                return this.sendAjaxError();
            }
        } catch (Exception e) {
            log.error("财务部审核不通过预付单异常{}", e);
            return this.sendAjaxError();
        }
        return this.sendAjaxOK();
    }

    /**
     * 采购部查询预付单列表
     *
     * @author 杜春静
     */
    @RequestMapping("queryPreBillList")
    public @ResponseBody
    BaseResponse queryPrepayBillList(@RequestBody XeVenFinPrepayBillQueryForm form) {
        log.info("查询预付单列表请求参数为：{}" + JSON.toJSONString(form));

        // 验证参数
        if (null == form) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        //判断时间是否超过3个月
        if (!StringUtils.isEmpty(form.getPrepayDateStart()) && !StringUtils.isEmpty(form.getPrepayDateEnd())) {
//  			boolean isOverThree = DateUtil.comparableTime(form.getCreateDateStart(),form.getCreateDateEnd());
//  			if(!isOverThree){
//  				return sendAjaxError(XeVenOrderMsg.TIME_MSG);
//  			}
            String createDateEnd = form.getPrepayDateEnd();
            form.setPrepayDateEnd(createDateEnd + " 23:59:59");
        }
        try {
            //1.获取userCode
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            //封装分页查询数据
            XePageInfo<XeVenFinPrepayBillView> xePageInfo;
            PageHelper.startPage(form.getPageNum(), form.getPageSize(), true);
            //查询预付单列表
            List<XeVenFinPrepayBillView> prepayBillList = prepayBillService.queryPrepayBillList(userInfo, form);
            xePageInfo = new XePageInfo<XeVenFinPrepayBillView>(prepayBillList);
            //查询总的预付款金额和单数
            Map<String, Object> map = new HashMap<String, Object>();
            map = prepayBillService.queryTotalPrepayAmount(userInfo, form);
            map.put("actionList", prepayBillList);
            map.put("xePageInfo", xePageInfo);
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("查询预付单列表异常{}", e);
            return this.sendAjaxError();
        }
    }

    /**
     * 根据预付单号查询预付单信息
     *
     * @author 杜春静
     */
    @RequestMapping("queryPreBillByCode")
    public @ResponseBody
    BaseResponse queryPrepayBillByCode(@RequestBody XeVenFinPrepayBillQueryForm form, BindingResult bindingResult) {
        log.info("查询预付单请求参数为：{}" + JSON.toJSONString(form));

        //验证参数
        if (null == form || StringUtils.isEmpty(form.getPrepayBillNo())) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        try {
            //1.获取userCode
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            //查询预付单列表
            List<XeVenFinPrepayBillView> prepayBillList = prepayBillService.queryPrepayBillList(userInfo, form);
            Map<String, Object> map = new HashMap<String, Object>();
            if (prepayBillList != null && prepayBillList.size() > 0) {
                map.put("prepayBill", prepayBillList.get(0));
            } else {
                return this.sendAjaxError();
            }
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("查询预付单异常{}", e);
            return this.sendAjaxError();
        }
    }

    /**
     * 财务部审核查询预付单列表
     *
     * @author 杜春静
     */
    @RequestMapping("queryFinCheckPreBillList")
    public @ResponseBody
    BaseResponse queryFinCheckPrepayBillList(@RequestBody XeVenFinPrepayBillQueryForm form) {
        log.info("查询预付单列表请求参数为：{}" + JSON.toJSONString(form));

        // 验证参数
        if (null == form) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        try {
            //1.获取userCode
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            //封装分页查询数据
            XePageInfo<XeVenFinPrepayBillView> xePageInfo;
            PageHelper.startPage(form.getPageNum(), form.getPageSize(), true);
            //查询预付单列表
            form.setOtherPrepayStatus(this.FINMANAGER);
            List<XeVenFinPrepayBillView> prepayBillList = prepayBillService.queryPrepayBillList(userInfo, form);
            xePageInfo = new XePageInfo<XeVenFinPrepayBillView>(prepayBillList);
            //查询总的预付款金额和单数
            Map<String, Object> map = new HashMap<String, Object>();
            map = prepayBillService.queryTotalPrepayAmount(userInfo, form);
            map.put("actionList", prepayBillList);
            map.put("xePageInfo", xePageInfo);
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("查询预付单列表异常{}", e);
            return this.sendAjaxError();
        }
    }

    /**
     * 财务部付款预付单列表查询
     *
     * @author 杜春静
     */
    @RequestMapping("queryFinPayPreBillList")
    public @ResponseBody
    BaseResponse queryFinPayPrepayBillList(@RequestBody XeVenFinPrepayBillQueryForm form) {
        log.info("查询预付单列表请求参数为：{}" + JSON.toJSONString(form));

        // 验证参数
        if (null == form) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        try {
            //1.获取userCode
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            //封装分页查询数据
            XePageInfo<XeVenFinPrepayBillView> xePageInfo;
            PageHelper.startPage(form.getPageNum(), form.getPageSize(), true);
            //查询预付单列表
            form.setOtherPrepayStatus(this.FINPAY);
            List<XeVenFinPrepayBillView> prepayBillList = prepayBillService.queryPrepayBillList(userInfo, form);
            xePageInfo = new XePageInfo<XeVenFinPrepayBillView>(prepayBillList);
            //查询总的预付款金额和单数
            Map<String, Object> map = new HashMap<String, Object>();
            map = prepayBillService.queryTotalPrepayAmount(userInfo, form);
            map.put("actionList", prepayBillList);
            map.put("xePageInfo", xePageInfo);
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("查询预付单列表异常{}", e);
            return this.sendAjaxError();
        }
    }

    /**
     * 财务部向供货商付款
     *
     * @author 杜春静
     */
    @RequestMapping("/finPayPreBill")
    public @ResponseBody
    BaseResponse finPayPreBill(@RequestBody @Validated XeVenFinPrepayCheckForm form, BindingResult bindingResult) {
        log.info("财务部向供货商付款请求参数为：{}" + JSON.toJSONString(form));

        // 验证参数
        if (bindingResult.hasErrors()) {
            return sendAjaxError(bindingResult.getFieldError().getDefaultMessage());
        }
        if (null == form || StringUtils.isEmpty(form.getPrepayBills()) || !("FINPAY").equals(form.getOperateType())) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        try {
            String operatorContent = "财务部向供货商付款";
            operatorContent += JSON.toJSONString(form);
            //1.获取userCode
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            //验证预付单是否是财务待审核未删除的单子
            Map<String, Object> param = new HashMap<String, Object>();
            param.put("userCode", userInfo.getUserCode());
            param.put("prepayBills", form.getPrepayBills());
            param.put("prepayStatus", VenPrepayStatusEnum.PREPAYING.getValue());
            param.put("supplierCode", form.getSupplierCode());
            int countPrepay = prepayBillService.countBillByParam(param);
            if (countPrepay < 0 || countPrepay != form.getPrepayBills().length) {
                return sendAjaxError(BASEMessage.PARAM_ERROR);
            }
            //批量付款预付单
            int result = prepayBillService.finPayPrepayBill(userInfo, form);
            this.insertPrepayMqLog(null, form.getPrepayBills().toString(), operatorContent);
            if (result < 0) {
                return this.sendAjaxError();
            }
        } catch (Exception e) {
            log.error("财务部向供货商付款异常{}", e);
            return this.sendAjaxError();
        }
        return this.sendAjaxOK();
    }


    /**
     * 根据供货商查询预付单可以关联的采购单号
     *
     * @author 杜春静
     */
    @RequestMapping("queryPurchOrdersBySupplier")
    public @ResponseBody
    BaseResponse queryPurchOrdersBySupplier(@RequestBody XeVenFinPrepayBillQueryForm form) {
        log.info("查据供货商查询预付单可以关联的采购单号请求参数为：{}" + JSON.toJSONString(form));

        // 验证参数
        if (null == form || StringUtils.isEmpty(form.getSupplierCode())) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        try {
            //1.获取userCode
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
//		userInfo.setUserCode("SH1709229762171");
            List<String> prepayBillList = prepayBillService.querySupplierPurchase(userInfo, form);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("actionList", prepayBillList);
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("据供货商查询预付单可以关联的采购单号异常{}", e);
            return this.sendAjaxError();
        }
    }

    /**
     * 查询预付单可以付款的供货商
     *
     * @author 杜春静
     */
    @RequestMapping("querySuppliersForPrepay")
    public @ResponseBody
    BaseResponse querySuppliersForPrepay(@RequestBody XeVenFinPrepayBillQueryForm form) {
        log.info("查询预付单可以付款的供货商请求参数为：{}" + JSON.toJSONString(form));

        try {
            //1.获取userCode
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            log.info("配置商户code值：-------->" + laUsercode);

            if (!StringUtils.isEmpty(laUsercode)) {
                if (!userInfo.getUserCode().equals(laUsercode)) {
                    return sendAjaxError(XeVenFinaceMsg.OPERATE_NO_PERMISSION);
                }
            }
            List<SuppliersForPrepayView> prepayBillList = prepayBillService.querySuppliersForPrepay(userInfo, form);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("actionList", prepayBillList);
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("查询预付单可以付款的供货商异常{}", e);
            return this.sendAjaxError();
        }
    }

    /**
     * 查询预付单需要打印的数据
     *
     * @author 杜春静
     */
    @RequestMapping("queryPrintPreBill")
    public @ResponseBody
    BaseResponse queryPrintPreBill(@RequestBody XeVenFinPrepayBillQueryForm form) {
        log.info("查询预付单需要打印的数据请求参数为：{}" + JSON.toJSONString(form));

        //验证参数
        if (null == form || StringUtils.isEmpty(form.getPrepayBillNo())) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }
        try {
            //1.获取userCode
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            //查询预付单列表
            XeVenFinPrepayBillPrintView prepayBill = prepayBillService.queryPrepayBill(userInfo, form);
            Map<String, Object> map = new HashMap<String, Object>();
            if (prepayBill != null) {
                map.put("prepayBill", prepayBill);
            } else {
                return this.sendAjaxError();
            }
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("查询预付单需要打印的数据异常{}", e);
            return this.sendAjaxError();
        }
    }

    /**
     * 采购部导出预付单列表
     *
     * @author 杜春静
     */
    @RequestMapping("exPrepayList")
    public @ResponseBody
    BaseResponse exPrepayList(@ModelAttribute XeVenFinPrepayBillQueryForm form,
                              HttpServletResponse response) {
        try {
            String operatorContent = "采购部导出预付单列表";
            operatorContent += JSON.toJSONString(form);
            String[] str = new String[]{"supplierName", "prepayBillNo", "prepayAmount", "purchCodes", "prepayDate",
                    "prepayType", "prepayStatus", "bankAccName", "bankAccCard", "accBank", "payRemark", "relationSatementNo"};
            form.setFieldArr(str);
            // 1、验证参数
            if (form.getFieldArr().length <= 0) {
                return sendAjaxError(BASEMessage.PARAM_ERROR);
            }
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            form.setUserCode(userInfo.getUserCode());

            //判断时间是否超过3个月
            if (!StringUtils.isEmpty(form.getPrepayDateStart()) && !StringUtils.isEmpty(form.getPrepayDateEnd())) {
//	  			boolean isOverThree = DateUtil.comparableTime(form.getCreateDateStart(),form.getCreateDateEnd());
//	  			if(!isOverThree){
//	  				return sendAjaxError(XeVenOrderMsg.TIME_MSG);
//	  			}
                String createDateEnd = form.getPrepayDateEnd();
                form.setPrepayDateEnd(createDateEnd + " 23:59:59");
            }

            // 2.获取数据
            List<XeVenFinPrepayBillExDTO> finPurchPayEList = prepayBillService.exportPrepayList(userInfo, form);
            if (null != finPurchPayEList && finPurchPayEList.size() > 0) {
                // 3.导出
                boolean rs = XeVenExcelUtil.exportExcelService(finPurchPayEList, new XeVenFinPrepayBillExDTO(),
                        form.getFieldArr(), exportTempletDir, "PREPAYLIST-", response);
                this.insertPrepayMqLog(null, userInfo.getLoginName(), operatorContent);
                if (rs) {
                    return sendAjaxOK();
                } else {
                    return sendAjaxError();
                }
            } else {
                return sendAjaxOK("导出数据为空！");
            }
        } catch (Exception e) {
            log.error("预付单列表接口异常：{}", e);
            return sendAjaxError();
        }

    }

    /**
     * 财务部审核导出预付单列表
     *
     * @author 杜春静
     */
    @RequestMapping("exFinPrepayList")
    public @ResponseBody
    BaseResponse exFinPrepayList(@ModelAttribute XeVenFinPrepayBillQueryForm form,
                                 HttpServletResponse response) {
        try {
            String operatorContent = "财务部审核导出预付单列表";
            operatorContent += JSON.toJSONString(form);

            String[] str = new String[]{"supplierName", "prepayBillNo", "prepayAmount", "purchCodes", "prepayDate",
                    "prepayType", "prepayStatus", "bankAccName", "bankAccCard", "accBank", "payRemark", "relationSatementNo"};
            form.setFieldArr(str);
            // 1、验证参数
            if (form.getFieldArr().length <= 0) {
                return sendAjaxError(BASEMessage.PARAM_ERROR);
            }
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            form.setUserCode(userInfo.getUserCode());
            form.setOtherPrepayStatus(this.FINMANAGER);
            //判断时间是否超过3个月
            if (!StringUtils.isEmpty(form.getPrepayDateStart()) && !StringUtils.isEmpty(form.getPrepayDateEnd())) {
//	  			boolean isOverThree = DateUtil.comparableTime(form.getCreateDateStart(),form.getCreateDateEnd());
//	  			if(!isOverThree){
//	  				return sendAjaxError(XeVenOrderMsg.TIME_MSG);
//	  			}
                String createDateEnd = form.getPrepayDateEnd();
                form.setPrepayDateEnd(createDateEnd + " 23:59:59");
            }

            // 2.获取数据
            List<XeVenFinPrepayBillExDTO> finPurchPayEList = prepayBillService.exportPrepayList(userInfo, form);
            if (null != finPurchPayEList && finPurchPayEList.size() > 0) {
                // 3.导出
                boolean rs = XeVenExcelUtil.exportExcelService(finPurchPayEList, new XeVenFinPrepayBillExDTO(),
                        form.getFieldArr(), exportTempletDir, "FINPREPAYLIST-", response);
                this.insertPrepayMqLog(null, userInfo.getLoginName(), operatorContent);
                if (rs) {
                    return sendAjaxOK();
                } else {
                    return sendAjaxError();
                }
            } else {
                return sendAjaxOK("导出数据为空！");
            }
        } catch (Exception e) {
            log.error("财务审核预付单列表接口异常：{}", e);
            return sendAjaxError();
        }

    }

    /**
     * 财务部付款导出预付单列表
     *
     * @author 杜春静
     */
    @RequestMapping("exFinPrepayToPayList")
    public @ResponseBody
    BaseResponse exFinPrepayToPayList(@ModelAttribute XeVenFinPrepayBillQueryForm form,
                                      HttpServletResponse response) {
        try {
            String operatorContent = "财务部审核导出预付单列表";
            operatorContent += JSON.toJSONString(form);

            String[] str = new String[]{"supplierName", "prepayBillNo", "prepayAmount", "purchCodes", "prepayDate",
                    "prepayType", "prepayStatus", "bankAccName", "bankAccCard", "accBank", "payRemark", "relationSatementNo"};
            form.setFieldArr(str);
            // 1、验证参数
            if (form.getFieldArr().length <= 0) {
                return sendAjaxError(BASEMessage.PARAM_ERROR);
            }
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            form.setUserCode(userInfo.getUserCode());
            form.setOtherPrepayStatus(this.FINPAY);
            //判断时间是否超过3个月
            if (!StringUtils.isEmpty(form.getPrepayDateStart()) && !StringUtils.isEmpty(form.getPrepayDateEnd())) {
//	  			boolean isOverThree = DateUtil.comparableTime(form.getCreateDateStart(),form.getCreateDateEnd());
//	  			if(!isOverThree){
//	  				return sendAjaxError(XeVenOrderMsg.TIME_MSG);
//	  			}
                String createDateEnd = form.getPrepayDateEnd();
                form.setPrepayDateEnd(createDateEnd + " 23:59:59");
            }

            // 2.获取数据
            List<XeVenFinPrepayBillExDTO> finPurchPayEList = prepayBillService.exportPrepayList(userInfo, form);
            if (null != finPurchPayEList && finPurchPayEList.size() > 0) {
                // 3.导出
                boolean rs = XeVenExcelUtil.exportExcelService(finPurchPayEList, new XeVenFinPrepayBillExDTO(),
                        form.getFieldArr(), exportTempletDir, "FINTOPAYPREPAYLIST-", response);
                this.insertPrepayMqLog(null, userInfo.getLoginName(), operatorContent);
                if (rs) {
                    return sendAjaxOK();
                } else {
                    return sendAjaxError();
                }
            } else {
                return sendAjaxOK("导出数据为空！");
            }
        } catch (Exception e) {
            log.error("财务部付款导出预付单列表接口异常：{}", e);
            return sendAjaxError();
        }

    }

    /**
     * 更新预付单的打印状态
     *
     * @author 杜春静
     */
    @RequestMapping("/updatePrepayBillPrint")
    public @ResponseBody
    BaseResponse updatePrepayBillPrint(@RequestBody XeVenFinPrepayBillForm form) {
        log.info("更新预付单的打印状态请求参数为：{}" + JSON.toJSONString(form));
        // 验证参数
        if (null == form || StringUtils.isEmpty(form.getPrepayBillNo())) {
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        }

        //1.获取userCode
        try {
            String operatorContent = "采购部审核通过";
            operatorContent += JSON.toJSONString(form);
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            int result = prepayBillService.editPrepayBill(userInfo, form);
            this.insertPrepayMqLog(null, userInfo.getLoginName(), operatorContent);
            if (result > 0) {
                return this.sendAjaxOK();
            } else {
                return this.sendAjaxError();
            }
        } catch (Exception e) {
            log.error("更新预付单的打印状态出现异常：{}", e);
            return sendAjaxError();
        }

    }

    /**
     * 添加预付单中心日志
     *
     * @author 杜春静
     */
    private void insertPrepayMqLog(String logType, String key, String operatorContent) {
        try {
            String ipAddress = RequestUtil.getClientIpAddress(request);
            if (StringUtils.isEmpty(logType)) {
                logType = ErpLogTypeEnum.erpPrepayBillCenter.getValue();
            }
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            String userCode = userInfo.getUserCode();
            log.info("预付单中心MQ日志插入");
            OpertorLogUtil.opertorLog.sendOperatorLogByParam(ipAddress, key, logType, userInfo.getUserName(), operatorContent, userCode);
        } catch (Exception e) {
            log.error("预付单中心MQ日志插入失败,logType={},operatorContent={}", logType, operatorContent);
        }
    }
}
  
